package com.xframe.domain.views.builder;

import cn.hutool.json.JSONUtil;
import com.xframe.domain.emnus.Constant;
import com.xframe.domain.emnus.PublicFieldEnum;
import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.sys.SysModel;
import com.xframe.domain.sys.SysModelAttr;
import com.xframe.domain.views.ui.*;
import com.xframe.xdal.core.annotation.XQueryCondition;
import com.xframe.xdal.core.annotation.XSelectItemCollect;
import com.xframe.xdal.core.model.mapping.IColumnMapping;
import com.xframe.xdal.core.model.mapping.IRelation;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.util.StringUtil;
import com.xframe.xdal.core.util.XReflect;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 生成实体的编辑界面的视图
 * @ClassName: FormByAddUIModelBuilder
 * @Author: xufeng
 * @CreateTime: 2023/12/06
 **/
@Log4j2
public class FormByAddUIModelBuilder extends UIModelBuilder {
    static final String VIEW_NAME = "addView";
    static final String CONFIG_TYPE = "add";
    static final String RENDER_CLASS = "xForm";

    @Override
    public String getViewName() {
        return VIEW_NAME;
    }

    @Override
    public String getConfigType() {
        return CONFIG_TYPE;
    }

    @Override
    public String getRenderClass() {
        return RENDER_CLASS;
    }

    @Override
    public String setSysModelViewConfigContent(SysModel sysModel, IUserCredentials userCredentials) {

        Map<String, XQueryCondition> map = XReflect.getFieldAnnotationMap(sysModel.getModelDataType(), XQueryCondition.class);
        // 找出所有的非公有字段
        List<SysModelAttr> sysModelAttrList = SysModelAttr.findFromAttr(sysModel.getModelAttrList());
        List<FormUIModel> elements = new ArrayList<>();
        for(SysModelAttr modelAttr : sysModelAttrList){
            // 判断是不是引用字段
            IRelation relation = findRefAttr(modelAttr,sysModel);
            String attrType = modelAttr.getType();
            FormUIModel formUIModel = new FormUIModel();
            formUIModel.setAttrName(modelAttr.getAttrName());
            formUIModel.setProp(modelAttr.getAttrName());
            formUIModel.setDefValue(StringUtil.EMPTY);
            formUIModel.setLabel(DynamicBill.NAME_SIGE + modelAttr.getAttrName() + DynamicBill.NAME_SIGE_LABEL);
            formUIModel.setPlaceholder(DynamicBill.NAME_SIGE + modelAttr.getAttrName() + DynamicBill.NAME_SIGE_HOLDER);
            formUIModel.setRemark(modelAttr.getDesc());
            formUIModel.setDisabled(false);
            formUIModel.setMultiple(false);
            formUIModel.setShow(true);
            formUIModel.setRuleList(new ArrayList<>());
            formUIModel.setSortNo(modelAttr.getSortNo());
            formUIModel.setUpdateFlag(true);
            if(modelAttr.getUniqueFlag()){
                formUIModel.setUpdateFlag(false);
            }



            if(relation != null) {
                // 是引用类型 需要生成下拉框的查询条件
                //XSelectItemCollect xSelectItemCollect = TableInfoBuilder.findXSelectItem(relation.getDataType());
                formUIModel.setComponentName(UIType.SELECT.getComponentName());
                DataSourceOption dataSourceOption = new DataSourceOption();
                dataSourceOption.setDataSourceCode(relation.getDataType().getName());
                dataSourceOption.setOptionType(OptionType.DYNAMICS.getValue());
                dataSourceOption.setOptionName(XSelectItemCollect.OPTION_NAME);
                dataSourceOption.setOptionId(XSelectItemCollect.OPTION_ID);
                dataSourceOption.setQueryKey(DynamicBill.SELECT);
                dataSourceOption.setFilter(QueryUIModel.DELFLAG_FILTER);

                XQueryCondition queryCondition = map.get(modelAttr.getAttrName());

                if(queryCondition != null && StringUtil.isNotEmpty(queryCondition.valueCode())){
                    dataSourceOption.setFilter(QueryUIModel.DELFLAG_FILTER + ";valCode:EQ:" + queryCondition.valueCode());
                    dataSourceOption.setSorts(queryCondition.sorts());
                }

                formUIModel.setDataSourceOption(dataSourceOption);
            }else{
                if(modelAttr.getAttrName().equals(PublicFieldEnum.VERSION.getValue()) || modelAttr.getPrimaryKeyFlag()){
                    // 如果是主键或者版本号
                    formUIModel.setComponentName(UIType.NONE.getComponentName());
                    formUIModel.setShow(false);
                } else if(modelAttr.getAttrName().equals(PublicFieldEnum.DEL_FLAG.getValue())){
                    formUIModel.setComponentName(UIType.SELECT.getComponentName());
                    DataSourceOption dataSourceOption = new DataSourceOption();
                    dataSourceOption.setDataSourceCode(PublicFieldEnum.DEL_FLAG.getValue());
                    dataSourceOption.setOptionType(OptionType.STATIC.getValue());
                    dataSourceOption.setQueryKey(StringUtil.EMPTY);
                    formUIModel.setDefValue("0");
                    formUIModel.setDataSourceOption(dataSourceOption);
                } else if(attrType.equals(java.util.Date.class.getName())){
                    formUIModel.setComponentName(UIType.DATE.getComponentName());
                }else {
                    formUIModel.setComponentName(UIType.TEXT.getComponentName());
                    formUIModel.setMaxLength(modelAttr.getLength());
                    if(!modelAttr.getNullFlag()){
                        // 如果不能为空 生成必填校验
                        UIRule uiRule = new UIRule();
                        uiRule.setRequired(true);
                        uiRule.setMessage(DynamicBill.NAME_SIGE + modelAttr.getAttrName() + DynamicBill.RULE_SIGE_RULE);
                        formUIModel.getRuleList().add(uiRule);
                    }
                }
            }
            elements.add(formUIModel);
        }
        DynamicBill dynamicBill = new DynamicBill();
        dynamicBill.setTitle(DynamicBill.NAME_SIGE_TITLE);
        dynamicBill.setModelCode(sysModel.getModelCode());
        dynamicBill.setElements(elements);
        dynamicBill.setConfigType(this.getConfigType());
        String json = JSONUtil.toJsonStr(dynamicBill);
        return json;
    }
}
